<?php

session_start();

require( 'util.php' );

validate_action_get( array( 'pid' ) );
$pid = $_GET['pid'];

require( 'db.php' );

$sql = "SELECT user.u_id as uid, user.name as uname, user.birthday as uregtime, m_id, subject, message, dateline, comment_times, access_times FROM page, user WHERE p_id = $pid AND page.u_id = user.u_id";
$result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );
$thread = mysql_fetch_array( $result ) or report_err_and_exit( '指定帖子不存在。' );

$mid = $thread['m_id'];
$model = get_model( $mid );

// 更新查看次数
$sql = "UPDATE `page` SET `access_times` =  `access_times` + 1 WHERE p_id = $pid";
$result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );

// TDOO:
$page = 1;

////////////////////////////////////////////////////////////

function strip(&$str)
{
	$str = str_replace( '\"', '"', $str );
	$str = str_replace( "\'", "'", $str );
}

function output_page_control_bar()
{
	global $mid, $page;
	echo <<<ROD
<div id="pgt" class="pgs mbm cl ">
<div class="pgt"></div>
<span class="y pgb"><a href="list.php?&mid=$mid&page=$page">返回列表</a></span>
ROD;

    if (can_post_new_page())
    {
		echo <<<ROD
<a id="newspecial" title="发新帖" href="new_page.php?mid=$mid&mode=write"><img src="static/image/common/pn_post.png" alt="发新帖" /></a>
ROD;
	}
	
	if (is_logged())
	{
		echo <<<ROD
<a name="post_reply" title="回复">
<img src="static/image/common/pn_reply.png" alt="回复" />
</a>
ROD;
	}
	else
	{
		echo '<div style="height:43px">&nbsp;</div>';
	}
    
    echo '</div>';
}

function output_post($post)
{
	$user_space_url = 'javascript:;';
	
	if ($post['floor'] == '楼主')
	{
		$replay_attr = 'name="post_reply"';
		$del_url = 'delete_page.php?pid=' . $GLOBALS['pid'];
		
		$post['c_id'] = 0;
	}
	else
	{
		$replay_attr = <<<ROD
href="javasciprt:;" onclick="fastReply({$post['c_id']});return false;"
ROD;

		$del_url = "delete_comment.php?cid={$post['c_id']}&pid={$GLOBALS['pid']}";
	}
	
	$avatar_url = 'userphoto/' . $post['uid'];
	if (!file_exists( $avatar_url ))
		$avatar_url = 'images/touxiang.jpg';
	
	echo <<<ROD
<div>
<table cellspacing="0" cellpadding="0">
<tr>
<td class="pls" rowspan="2">

<div class="pi">
<div class="authi">
<a href="$user_space_url" id="xuname{$post['c_id']}" target="_blank" class="xw1">{$post['uname']}</a>
</div>
</div>

<div class="p_pop blk bui" style="display: none; margin-top: -11px;">
<div class="m z">
</div>
<div class="i y">
<div>
<strong><a href="$user_space_url" target="_blank" class="xi2">{$post['uname']}</a></strong>
</div>

</div>
</div>

<div class="avatar"><a href="$user_space_url" target="_blank">
<img src="$avatar_url" width="100px" height="100px" /></a>
</div>

<dl class="pil cl">
	<dt>UID</dt><dd>{$post['uid']}</dd>
	<dt>注册时间</dt><dd>{$post['uregtime']}</dd>
</dl>

</td>
<td class="plc">
<div class="pi">
	<strong><a href="javascript:;"><em>{$post['floor']}</em></a></strong>
	<div class="pti"><div class="pdbt"></div>
		<div class="authi">
			<img class="authicn vm" src="static/image/common/online_member.gif" />
			<em>发表于 <span id="xposttime{$post['c_id']}">{$post['dateline']}</span></em>
		</div>
	</div>
</div>
<div class="pct"><style type="text/css">.pcb{margin-right:0}</style>
<div class="pcb">
	<div class="t_fsz">
		<table cellspacing="0" cellpadding="0">
			<tr><td class="t_f"><div id="xcomment{$post['c_id']}">{$post['message']}</div></td></tr>
		</table>
	</div>
	<div class="cm"></div>
	<div></div>
</div>
</div>

</td></tr>

<tr><td class="plc plm"></td></tr>

<tr>
	<td class="pls"></td>
	<td class="plc">
	<div class="po">
		<div class="pob cl">
			<em><a class="fastre" $replay_attr>回复</a></em>
ROD;

	if (is_logged_as_monitor()) {
		echo '<em><a class="del" href="'. $del_url . '">删除</a></em>';
	}
	
	echo <<<ROD
		</div>
	</div>
	</td>
</tr>

<tr class="ad"><td class="pls"></td><td class="plc"></td></tr>

</table>
</div>
ROD;
}

//********************** 页面内容开始 **********************

require_once( 'Stencil.php' );
output_frame_header( $thread['subject'] );

?>

<script type="text/javascript" src="jquery.js"></script>
<script charset="utf-8" src="kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="kindeditor/lang/zh_CN.js"></script>

<script>

    editor = null;
    $(function() {
        editor = KindEditor.create('#msg');
		$("a[name='post_reply']")
			.attr("href", $.browser.msie ? "#msg" : "#fpost")
			.click( function() {
				editor.focus();
			});
    });
	
	function fastReply(commentId) {
	
		var original = $("#xcomment" + commentId).html();
		original = original.replace( '\r', '' );
		original = original.replace( '\n', '' );
		original = original.replace( /\<div class\=.+?quote.+?\<\/blockquote\>\<\/div\>(\<br\>)?/ig, '' );
		//alert(original);
	
		var quote = '<div class="quote"><blockquote><font size="2"><font color="#999999">' 
                  + $("#xuname" + commentId).text() + ' 发表于 ' 
                  + $("#xposttime" + commentId).text() + '</font></font><br />' 
                  + original + '</blockquote></div><br />';

		editor.html( quote );
		editor.focus();
	}
    
</script>

<link rel="stylesheet" type="text/css" href="static/style/common.css" />
<link rel="stylesheet" type="text/css" href="static/style/forum_viewthread.css" />
<style>.del{background:url(static/image/common/del.png) no-repeat 4px 50%;}</style>

<?php

echo "<a href='list.php?mid=$mid'>", $model['topic'], '</a> &gt; ';
echo "<a href='view_page.php?pid=$pid'>", $thread['subject'], '</a><br /><br />', "\n";

output_page_control_bar();

?>

<div id="postlist" class="pl bm">

<table cellspacing="0" cellpadding="0">
	<tr>
		<td class="pls ptm pbm">
			<div class="hm">
			<span class="xg1">查看:</span> <span class="xi1"><?php echo $thread['access_times']; ?></span><span class="pipe">|</span><span class="xg1">回复:</span> <span class="xi1"><?php echo $thread['comment_times']; ?></span>
			</div>
		</td>
		<td class="plc ptm pbn">
			<h1 class="ts">
			<a href="view_page.php?pid=<?php echo $pid; ?>" id="thread_subject">
			<?php echo $thread['subject']; ?>
			</a>
			</h1>
		</td>
	</tr>
</table>

<!-- 一行蓝色条 -->
<table cellspacing="0" cellpadding="0" class="ad"><tr><td class="pls"></td><td class="plc"></td></tr></table>

<?php

// KindEditor 自动转义？
strip( $thread['message'] );
$thread['floor'] = '楼主';
output_post( $thread );

// 输出所有回复
$sql = "SELECT user.u_id as uid, user.name as uname, user.birthday as uregtime, comment.c_id, comment.message, comment.dateline FROM user, comment WHERE comment.p_id = $pid AND user.u_id = comment.u_id ORDER BY comment.dateline";
$result = mysql_query( $sql ) or report_err_and_exit( mysql_error() );

$floor = 2;
while ($comment = mysql_fetch_array( $result ))
{
    strip( $comment['message'] );
	$comment['floor'] = $floor++ . '楼';
	
	output_post( $comment );
}

?>

</div>
<div id="postlistreply" class="pl"><div id="post_new" class="viewthread_table" style="display: none"></div></div>

<?php

output_page_control_bar();

if (is_logged())
{
	$avatar_url = 'userphoto/' . $_SESSION['u_id'];
	if (!file_exists( $avatar_url ))
		$avatar_url = 'images/touxiang.jpg';
?>

<div id="f_pst" class="pl bm bmw">
<form id="commentEditor" method="POST" action="comment.php" onSubmit="return fastpostvalidate(this)">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="pls">
<div class="avatar"><img src="<?php echo $avatar_url; ?>" width="100px" height="100px" /></div></td>
<td class="plc">
<span id="fastpostreturn"></span>

<div class="cl">
<div class="area" id="fpost">
<textarea rows="6" cols="140" name="msg" id="msg" tabindex="4" class="pt"></textarea>
<input type="hidden" name="pid" value="<?php echo $pid; ?>" />
</div>
</div>

<p class="ptm pnpost">
<button type="submit" name="replysubmit" id="fastpostsubmit" class="pn pnc vm" value="replysubmit" tabindex="5"><strong>发表回复</strong></button>

</p>
</td>
</tr>
</table>
</form>
</div>

<script>

    $("#commentEditor").submit( function()
    {
        var msg = editor.html();
        var empty = (!msg || msg == "");
        
        if (!empty)
        {
            editor.sync();
            return true;
        }
        else
        {
            alert('评论内容不能为空。');
            editor.focus();
            
            return false;
        }
    });

</script>

<div style="height=14px">&nbsp</div>

<?php
}

//********************** 页面内容结束 **********************

output_frame_footer();

?>